using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using EntityKit.Runtime;

namespace EntityKit.Editor
{
    public class LocalizationQueryWindow : EditorWindow
    {
        private LocalizationConfig mSelectedConfig;
        private Vector2 mScrollViewPosition;
        private string mSearchKey = "";
        
        private Dictionary<string, string> mFilteredData = new Dictionary<string, string>();
        
        private GUIStyle mHeaderStyle;
        private GUIStyle mBoxStyle;
        private GUIStyle mLabelStyle;
        private GUIStyle mKeyStyle;
        private GUIStyle mValueStyle;
        
        public static void ShowWindow()
        {
            LocalizationQueryWindow window = GetWindow<LocalizationQueryWindow>("本地化查询窗口");
            window.minSize = new Vector2(600, 400);
        }

        private void OnEnable()
        {
            InitializeStyles();
        }

        private void InitializeStyles()
        {
            if (mHeaderStyle == null)
            {
                mHeaderStyle = new GUIStyle(EditorStyles.largeLabel);
                mHeaderStyle.fontStyle = FontStyle.Bold;
                mHeaderStyle.fontSize = 16;
            }

            if (mBoxStyle == null)
            {
                mBoxStyle = new GUIStyle(EditorStyles.helpBox);
            }

            if (mLabelStyle == null)
            {
                mLabelStyle = new GUIStyle(EditorStyles.label);
            }

            if (mKeyStyle == null)
            {
                mKeyStyle = new GUIStyle(EditorStyles.boldLabel);
            }

            if (mValueStyle == null)
            {
                mValueStyle = new GUIStyle(EditorStyles.textArea);
                mValueStyle.wordWrap = true;
                mValueStyle.stretchHeight = true;
            }
        }

        private void OnGUI()
        {
            InitializeStyles();

            EditorGUILayout.Space(10);
            EditorGUILayout.LabelField("本地化数据查询", mHeaderStyle);
            EditorGUILayout.HelpBox("选择一个本地化配置文件并在其中搜索键和值。", MessageType.Info);

            // 配置文件选择
            EditorGUILayout.BeginVertical(mBoxStyle);
            EditorGUILayout.LabelField("配置文件选择", EditorStyles.boldLabel);
            mSelectedConfig = (LocalizationConfig)EditorGUILayout.ObjectField("本地化配置", mSelectedConfig, typeof(LocalizationConfig), false);
            EditorGUILayout.EndVertical();

            if (mSelectedConfig == null)
            {
                EditorGUILayout.HelpBox("请选择一个本地化配置文件以查看数据。", MessageType.Warning);
                return;
            }


            DrawSearchOptions();
            DrawSearchResults();
        }

        private void DrawSearchOptions()
        {
            EditorGUILayout.BeginVertical(mBoxStyle);
            EditorGUILayout.LabelField("搜索选项", EditorStyles.boldLabel);

            // 搜索框
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("搜索键/值");
            mSearchKey = EditorGUILayout.TextField(mSearchKey);
            if (GUILayout.Button("搜索", GUILayout.Width(60)))
            {
                ApplyFilter();
            }
            EditorGUILayout.EndHorizontal();
            
            EditorGUILayout.Space(5);
            
            // 清空按钮
            if (GUILayout.Button("显示所有数据"))
            {
                ShowAllData();
            }

            EditorGUILayout.EndVertical();
        }

        private void ApplyFilter()
        {
            mFilteredData.Clear();

            // 在整个配置中搜索
            var entries = mSelectedConfig.GetAllEntries();
            foreach (var entry in entries)
            {
                // 检查键或值是否包含搜索关键词
                if (string.IsNullOrEmpty(mSearchKey) || 
                    entry.Key.Contains(mSearchKey) || 
                    entry.Value.Contains(mSearchKey))
                {
                    // 使用唯一键避免重复（语言_类型_键）
                    string uniqueKey = $"[{entry.Language}] {entry.Type}: {entry.Key}";
                    mFilteredData[uniqueKey] = entry.Value;
                }
            }
        }
        
        private void ShowAllData()
        {
            mSearchKey = "";
            ApplyFilter();
        }

        private void DrawSearchResults()
        {
            EditorGUILayout.LabelField($"结果数: {mFilteredData.Count}", EditorStyles.boldLabel);

            mScrollViewPosition = EditorGUILayout.BeginScrollView(mScrollViewPosition);
            foreach (var kvp in mFilteredData)
            {
                DrawLocalizationEntry(kvp.Key, kvp.Value);
            }
            EditorGUILayout.EndScrollView();
        }

        private void DrawLocalizationEntry(string key, string value)
        {
            EditorGUILayout.BeginVertical(mBoxStyle);
            EditorGUILayout.LabelField("键:", mKeyStyle);
            EditorGUILayout.SelectableLabel(key, mLabelStyle, GUILayout.Height(EditorGUIUtility.singleLineHeight * 1.2f));
            
            EditorGUILayout.Space(5);
            
            EditorGUILayout.LabelField("值:", mKeyStyle);
            EditorGUILayout.SelectableLabel(value, mValueStyle, GUILayout.Height(EditorGUIUtility.singleLineHeight * Mathf.Max(1.2f, value.Split('\n').Length)));
            
            EditorGUILayout.EndVertical();
            EditorGUILayout.Space(5);
        }
    }
}